const fs = require("fs");
const path = require('path');
const permissions = require("./permissions.json");

// 假定的输入对象
const testCases = permissions;


function generateTestCode(testCases) {
  const files = {};

  // 将测试用例按照文件分类并排序
  Object.keys(testCases).sort().forEach(key => {
    const prefix = key.split(/[-:]/)[0]; // 获取前缀，以 '-' 或 ':' 分割
    if (!files[prefix]) {
      files[prefix] = [];
    }
    files[prefix].push(key);
  });

  // 为每个分类生成测试代码
  Object.entries(files).forEach(([prefix, keys]) => {
    let code = `import { expect, hasAuth, test } from "../fixtures";\n\n`;
    code += `test.describe(\`auth: \${hasAuth ? "yes" : "no"}\`, () => {\n`;
    keys.forEach(key => {
      const descriptions = testCases[key];
      code += `  test.describe("${key}", () => {\n`;
      descriptions.forEach(desc => {
        code += `    test("${desc}", async ({ page }) => {\n`;
        code += `      expect(false).toBe(true);\n`;
        code += `    });\n`;
      });
      code += `  });\n`;
    });
    code += `});\n`;

    const filePath = path.join(__dirname, 'tests', `${prefix}.spec.ts`);
    fs.mkdirSync(path.dirname(filePath), { recursive: true });
    fs.writeFileSync(filePath, code);
    console.log(`File written: ${filePath}`);
  });
}

generateTestCode(testCases);